<template>
	<view class="new">
		<view class="navbar">
			<u-navbar title="消息中心" bgColor="transparent" :placeholder="true">
				<view slot="left"></view>
			</u-navbar>
		</view>
		<image class="top-bg" src="@/static/news/top_bg.png" mode=""></image>
		<view class="tabbar">
			<image class="tab-bg" src="@/static/news/tab_bg.png" mode=""></image>
			<view class="tabbar-list">
				<view class="tabbar-item" @click="chooseType(1)">
					<view class="number" v-if="score" :class="score.length > 2 ? 'number-more' : ''">
						<image v-if="score.length > 2" src="@/static/news/num_bg2.png" mode=""></image>
						<image v-else src="@/static/news/num_bg1.png" mode=""></image>
						<text>{{score.length > 2 ? '99+' : score}}</text>
					</view>
					<image class="tab-icon" src="@/static/news/tab_icon1.png" mode=""></image>
					<view class="text">
						<image v-if="type == 1" class="tab-text-bg" src="@/static/news/tab_text_bg.png" mode=""></image>
						<text>积分</text>
					</view>
				</view>
				<view class="tabbar-item" @click="chooseType(3)">
					<view class="number" v-if="jiedan" :class="jiedan.length > 2 ? 'number-more' : ''">
						<image v-if="jiedan.length > 2" src="@/static/news/num_bg2.png" mode=""></image>
						<image v-else src="@/static/news/num_bg1.png" mode=""></image>
						<text>{{jiedan.length > 2 ? '99+' : jiedan}}</text>
					</view>
					<image class="tab-icon" src="@/static/news/tab_icon2.png" mode=""></image>
					<view class="text">
						<image v-if="type == 3" class="tab-text-bg" src="@/static/news/tab_text_bg.png" mode=""></image>
						<text>接单</text>
					</view>
				</view>
				<view class="tabbar-item" @click="chooseType(2)">
					<view class="number" v-if="order" :class="order.length > 2 ? 'number-more' : ''">
						<image v-if="order.length > 2" src="@/static/news/num_bg2.png" mode=""></image>
						<image v-else src="@/static/news/num_bg1.png" mode=""></image>
						<text>{{order.length > 2 ? '99+' : order}}</text>
					</view>
					<image class="tab-icon" src="@/static/news/tab_icon3.png" mode=""></image>
					<view class="text">
						<image v-if="type == 2" class="tab-text-bg" src="@/static/news/tab_text_bg.png" mode=""></image>
						<text>订单</text>
					</view>
				</view>
			</view>
		</view>
		<view class="news-title">
			<text v-if="type == 1">积分消息</text>
			<text v-else-if="type == 3">接单消息</text>
			<text v-else-if="type == 2">订单消息</text>
			<text v-else>全部消息</text>
		</view>
		<scroll-view class="news-list" scroll-y :style="'height:calc(100vh - '+ offsetTop +')'" @scrolltolower="scrolltolower"
			v-if="messageLists.length > 0">
			<view class="news-item" v-for="(item,index) in messageLists" :key="index" @click="onJump(item)">
				<image class="item-bg" src="@/static/news/item_bg.png" mode=""></image>
				<view class="news-icons">
					<image class="news-icon" v-if="item.is_type == 1" src="@/static/news/icon1.png" mode=""></image>
					<image class="news-icon" v-if="item.is_type == 3" src="@/static/news/icon2.png" mode=""></image>
					<image class="news-icon" v-if="item.is_type == 2" src="@/static/news/icon3.png" mode=""></image>
				</view>
				<view class="contetn">
					<view class="title">
						<text v-if="item.is_type == 1">积分变动</text>
						<text v-if="item.is_type == 3">接单消息</text>
						<text v-if="item.is_type == 2">订单消息</text>
						<text>{{item.createtime | formattingTime}}</text>
					</view>
					<view class="text">
						{{item.message}}
					</view>
				</view>
				<image v-if="item.is_user_del == 0" class="arrow-right" src="@/static/news/reading_icon.png" mode="">
				</image>
			</view>
		</scroll-view>
		<dwu-empty image="/static/public/empty_icon1.png" imageW="234rpx" imageH="166rpx" tips="暂无消息~"
			v-else></dwu-empty>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				score: 0,
				order: 0,
				jiedan: 0,
				type: '',
				messageLists: [],
				page: 1,
				pageSize: 10,
				flag: false,
				offsetTop: 0
			};
		},
		onShow() {
			this.getHeight()
		},
		onLoad() {
			this.messageIndex()
			this.messageList()
		},
		methods: {
			getHeight() {
				const query = uni.createSelectorQuery().in(this);
				query.select('.navbar').boundingClientRect(data => {
					if (data) {
						this.offsetTop = ((data.height * 2) + 300) + 'rpx'
						this.$forceUpdate()
					}
				}).exec();
			},
			scrolltolower() {
				// if (this.flag) return
				// this.page++
				// this.flag = true
				// this.messageList(true)
			},
			chooseType(type) {
				this.type = type
				this.page = 1
				this.flag = false
				this.messageList()
			},
			messageIndex() {
				this.request.http('/user/messageIndex', 'post').then(res => {
					let data = res.data
					if (data.code == 1) {
						this.score = data.data.score
						this.order = data.data.order
						this.jiedan = data.data.jiedan
					}
				})
			},
			messageList(next) {
				this.request.http('/user/messageList', 'post', {
					is_type: this.type,
					page: this.page,
					limit: this.pageSize,
				}).then(res => {
					let data = res.data
					if (data.code == 1) {
						if (next) {
							if (data.data.length > 0) {
								this.messageLists = this.messageLists.concat(data.data)
								this.flag = false
							}
						} else {
							this.messageLists = data.data
							this.flag = false
						}
					}
				})
			},
			onJump(item) {
				this.request.http('/user/eliminate', 'post', {
					id: item.id
				}).then(res => {
					let data = res.data
					if (data.code == 1) {
						item.is_user_del = 1
						this.messageIndex()
					}
				})

				if (item.is_type == 1) {
					uni.navigateTo({
						url: '/subpackage/integral/integral'
					})
				}
				if (item.is_type == 2) {
					uni.navigateTo({
						url: '/subpackage/order/detail?id=' + item.order_id
					})
				}
				if (item.is_type == 3) {
					uni.navigateTo({
						url: '/subpackage/order/detail?id=' + item.order_id
					})
				}
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #F7F9F9;
	}

	.new {
		.top-bg {
			position: fixed;
			z-index: -1;
			top: 0;
			left: 0;
			width: 100%;
			height: 792rpx;
		}

		.tabbar {
			position: relative;
			padding: 0 32rpx;

			.tab-bg {
				position: absolute;
				z-index: 1;
				left: 0;
				top: 0;
				width: 100%;
				height: 228rpx;
			}

			.tabbar-list {
				padding-top: 48rpx;
				position: relative;
				z-index: 10;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.tabbar-item {
					flex: 1;
					display: flex;
					align-items: center;
					justify-content: center;
					flex-direction: column;
					position: relative;

					.number {
						position: absolute;
						top: 0;
						right: 30rpx;
						z-index: 11;
						width: 36rpx;
						height: 34rpx;

						&.number-more {
							width: 52rpx;
							height: 32rpx;
							right: 16rpx;
						}

						image {
							position: absolute;
							z-index: 1;
							width: 100%;
							height: 100%;
						}

						text {
							display: block;
							position: relative;
							z-index: 10;
							text-align: center;
							line-height: 34rpx;
							font-family: PingFangSC, PingFang SC;
							font-weight: 500;
							font-size: 21rpx;
							color: #FFFFFF;
						}

					}

					.tab-icon {
						width: 112rpx;
						height: 118rpx;
					}

					.text {
						margin-top: -20rpx;
						position: relative;
						width: 112rpx;
						height: 58rpx;
						text-align: center;
						padding-left: 20rpx;

						.tab-text-bg {
							position: absolute;
							z-index: 1;
							top: 0;
							left: 10rpx;
							width: 112rpx;
							height: 58rpx;
						}

						text {
							position: relative;
							z-index: 10;
							display: block;
							font-family: PingFangSC, PingFang SC;
							font-weight: 500;
							font-size: 23rpx;
							color: #666666;
							line-height: 58rpx;
						}
					}
				}
			}
		}

		.news-title {
			margin-top: 46rpx;
			padding-left: 48rpx;
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 31rpx;
			color: #474747;
			line-height: 42rpx;
		}

		.news-list {
			.news-item {
				position: relative;
				padding: 48rpx 58rpx 58rpx 58rpx;
				display: flex;
				align-items: center;
				position: relative;

				.item-bg {
					position: absolute;
					z-index: 1;
					width: 100%;
					height: 100%;
					top: 0;
					left: 0;
				}

				.news-icons {
					margin-right: 24rpx;
					position: relative;
					z-index: 10;
					width: 82rpx;
					height: 82rpx;
					flex-shrink: 0;

					.news-icon {
						width: 100%;
						height: 100%;
					}
				}

				.contetn {
					position: relative;
					z-index: 10;
					flex: 1;
					// margin-right: 30rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					height: 82rpx;

					.title {
						display: flex;
						align-items: center;
						justify-content: space-between;

						text {
							font-family: PingFangSC, PingFang SC;
							font-weight: 500;
							font-size: 27rpx;
							color: #333333;

							&:last-child {
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 23rpx;
								color: #999999;
							}
						}
					}

					.text {
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 23rpx;
						color: #999999;
					}
				}

				.arrow-right {
					position: absolute;
					top: 50%;
					transform: translateY(-50%);
					right: 063rpx;
					z-index: 10;
					flex-shrink: 0;
					width: 16rpx;
					height: 16rpx;
				}

			}
		}
	}
</style>